package com.czf.CurriculumVitae.db;

import com.czf.CurriculumVitae.bean.Admin;
import com.czf.CurriculumVitae.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author chen
 * @Description: 用户登录
 * @date 2021/3/25 16:37
 */
public class LoginDaoImp implements LoginDao<Admin> {

    private static final String SQL_FIND_BY_USERID = "select * from kkb_login where username=? and password=?";

    @Override
    public Admin userLogin(Admin admin) {
        Connection conn = DBUtil.getConn();
        PreparedStatement stat = null;

        try {
            stat = conn.prepareStatement(SQL_FIND_BY_USERID);
            stat.setString(1, admin.getUsername());
            stat.setString(2, admin.getPassword());

            ResultSet resultSte = stat.executeQuery();

            while (resultSte.next()) {
                String id = resultSte.getString("id");
                String username = resultSte.getString("username");
                String password = resultSte.getString("password");
                Admin userLogin = new Admin(id, username, password);
                return userLogin;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(conn, stat, null);
        }
        return null;
    }
}
